<?php
    
	require_once( 'shortcode-option.php' );
    
	/**
	 * initialization
	 */
	define('CV_SHORTCODE_VERSION', '1.0.1');	
	
	function cv_shortcode_init() {			
		$sc_option = cv_get_option(); 

		// Load libs
		require_once( dirname( __FILE__ ) . '/source/available.php' );		
		require_once( dirname( __FILE__ ) . '/source/shortcodes.php' );

		// Enable shortcodes in text widgets
		add_filter( 'widget_text', 'do_shortcode' );
			
		
		// Enable auto-formatting
		if ( $sc_option[ 'sc_disable_customformatting'] != true ) 
		{			
			// Disable WordPress native formatters
			remove_filter( 'the_content', 'wpautop' );
			remove_filter( 'the_content', 'wptexturize' );

			// Apply custom formatter function
			add_filter( 'the_content', 'cv_custom_formatter', 99 );
			add_filter( 'widget_text', 'cv_custom_formatter', 99 );
		}

		// Fix for large posts, http://core.trac.wordpress.org/ticket/8553
		@ini_set( 'pcre.backtrack_limit', 500000 ); 
		
		// Register styles
		wp_register_style( 'style-shortcodes', cv_shortcode_url() . '/css/shortcode.css');		
		wp_register_style( 'style-shortcodes-generator', cv_shortcode_url() . '/css/generator.css');
	

		// Register scripts
		wp_register_script( 'js-shortcodes', cv_shortcode_url() . '/js/shortcode.js', array( 'jquery' ));	
		wp_register_script( 'js-shortcodes-generator', cv_shortcode_url() . '/js/generator.js', array( 'jquery' ));
	

		// Front-end scripts and styles
		if ( !is_admin() ) {			
			
			wp_enqueue_script( 'js-shortcodes');
			wp_enqueue_style( 'style-shortcodes' );
			
		}


		// Scipts and stylesheets for editing pages (shortcode generator popup)
		if ( is_admin() ) {
			// Get current page type
			global $pagenow;

			// Pages for including
			$cv_generator_includes_pages = array( 'post.php', 'edit.php', 'post-new.php', 'index.php' );

			if ( in_array( $pagenow, $cv_generator_includes_pages ) ) {
				// Enqueue styles
				wp_enqueue_style( 'style-shortcodes-generator' );

				// Enqueue scripts
				wp_enqueue_script( 'js-shortcodes-generator' );
			}
		}

		// Register shortcodes
		foreach ( cv_shortcodes() as $shortcode => $params ) {
			add_shortcode( cv_compatibility_mode_prefix() . $shortcode, 'cv_' . $shortcode . '_shortcode' );
		}	 
	}
	
	add_action( 'init', 'cv_shortcode_init', 1);
	
	/**
	 * Returns current shortcode version.
	 *
	 * @return string shortcode version
	 */
	function cv_get_version() {		
		return CV_SHORTCODE_VERSION;
	}

	/**
	 * Returns current shortcode url
	 *
	 * @return string shortcode url
	 */
	function cv_shortcode_url() {
		return CVINCLUDES_SHORTCODE_URI;
	}

	/**
	 * Shortcode names prefix in compatibility mode
	 *
	 * @return string Special prefix
	 */
	function cv_compatibility_mode_prefix() {
		$data = cv_get_option();
		$prefix = ( $data['sc_compatibility_mode'] == true ) ? 'cv_' : '';
		return $prefix;
	}

	/**
	 * Disable auto-formatting for shortcodes
	 *
	 * @param string $content
	 * @return string Formatted content with clean shortcodes content
	 */
	function cv_custom_formatter( $content ) {
		
		$new_content = '';

		// Matches the contents and the open and closing tags
		$pattern_full = '{(\[raw\].*?\[/raw\])}is';

		// Matches just the contents
		$pattern_contents = '{\[raw\](.*?)\[/raw\]}is';

		// Divide content into pieces
		$pieces = preg_split( $pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE );

		// Loop over pieces
		foreach ( $pieces as $piece ) {

			// Look for presence of the shortcode
			if ( preg_match( $pattern_contents, $piece, $matches ) ) {

				// Append to content (no formatting)
				$new_content .= $matches[1];
			} else {

				// Format and append to content
				$new_content .= wptexturize( wpautop( $piece ) );
			}
		}

		return $new_content;
	}


	/**
	 * Add generator button to Upload/Insert buttons
	 */
	function cv_add_generator_button() {
		echo '<a href="#TB_inline?width=640&height=800&inlineId=cv-generator-wrap" class="thickbox" title="' . __( 'Insert shortcode', hybrid_get_textdomain() ) . '"><img src="' . cv_shortcode_url() . '/images/admin/media-icon.png" alt="" /></a>';
	}

	add_action( 'media_buttons', 'cv_add_generator_button', 100 );	

	/**
	 * Generator popup box
	 */
	function cv_generator_popup() {
		?>
		<div id="cv-generator-wrap" style="display:none">
			<div id="cv-generator">
				<div id="cv-generator-shell">
					<div id="cv-generator-header">
						<select id="cv-generator-select">
							<option value="raw"><?php _e( 'Select shortcode', hybrid_get_textdomain() ); ?></option>
							<?php
							foreach ( cv_shortcodes() as $name => $shortcode ) {
								?>
								<option value="<?php echo $name; ?>"><?php echo strtoupper( $name ); ?>:&nbsp;&nbsp;&nbsp;<?php echo $shortcode['desc']; ?></option>
								<?php
							}
							?>
						</select>
						<div id="cv-generator-tools">
							<a href="<?php echo admin_url( 'themes.php?page=optionsframework' ); ?>" target="_blank" title="<?php _e( 'Settings', hybrid_get_textdomain() ); ?>"><img src="<?php echo cv_shortcode_url(); ?>/images/generator/settings.png" alt="" /></a>
							<a href="http://colorvila.com/forum/" target="_blank" title="<?php _e( 'Support forum', hybrid_get_textdomain() ); ?>"><img src="<?php echo cv_shortcode_url(); ?>/images/generator/support.png" alt="" /></a>
						</div>
					</div>
					<div id="cv-generator-settings"></div>
					<input type="hidden" name="cv-generator-url" id="cv-generator-url" value="<?php echo cv_shortcode_url(); ?>" />
					<input type="hidden" name="cv-compatibility-mode-prefix" id="cv-compatibility-mode-prefix" value="<?php echo cv_compatibility_mode_prefix(); ?>" />
				</div>
			</div>
		</div>
		<?php
	}

	add_action( 'admin_footer', 'cv_generator_popup' );

?>
